package value

import (
	"context"
	"fmt"
	"tongquetai_server/activity_vk/domain/manage/common/static_data"
)

const (
	GiftContentPackageType = 4 // 礼包类型
)
// @META.table:json
/*
[
    ["礼包内容配置"],
    ["礼包名称", "奖励内容", "奖励内容", "奖励内容"],
    ["筑守坚城现金礼包·一", "金币*1", "金币*1", "金币*1"],
    ["筑守坚城现金礼包·二", "金币*1", "金币*1", "金币*1"]
]
*/

// @META.struct:tqt
type TqtGiftContent struct {
	PackageName string     `json:"packageName" header:"礼包名称" validate:"required"` // 礼包名称
	No          int        `json:"no" skipField:"true" validate:"gt=0"`
	Award       []TqtAward `json:"award" header:"奖励内容" validate:"gt=0,dive"`
}

func (t *TqtGiftContent) Call(ctx context.Context) (err error) {
	// 校验并填充礼包编号
	actType := ctx.Value("actType").(int)
	activityItemNameSet, err := static_data.GetActivityItem().
		GetPackageNameMapByTypAndContainEffectTyp(ctx, actType, GiftContentPackageType)
	if err != nil {
		return
	}
	// 礼包名称只能与静态表名称对应
	info, ok := activityItemNameSet[t.PackageName]
	if !ok {
		err = fmt.Errorf("礼包名称:[%s]只能填写道具表中属于礼包类型的道具名", t.PackageName)
		return
	}
	t.No = info.No
	return
}

// @META.struct:svr
type SvrGiftContent struct {
	No    int        `json:"no"`
	Award []SvrAward `json:"award"`
}

// ------------------------------------ 适配器部分 -----------------------------------

// ToTqtGiftContent 转铜雀台
func ToTqtGiftContent(ctx context.Context, actType int, svrGiftContent []SvrGiftContent) (tqtGiftContent []TqtGiftContent, err error) {
	activityItemNoMap, err := static_data.GetActivityItem().
		GetPackageNoMapByTypAndContainEffectTyp(ctx, actType, GiftContentPackageType)
	if err != nil {
		return
	}
	tqtGiftContent = make([]TqtGiftContent, 0, len(svrGiftContent))
	for _, item := range svrGiftContent {
		tqtGiftContent = append(tqtGiftContent, TqtGiftContent{
			No:          item.No,
			PackageName: activityItemNoMap[item.No].Name,
			Award:       SvrToTqtAwards(ctx, item.Award),
		})
	}
	return
}
